package com.huatai.vba.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huatai.vba.common.exception.BusinessException;
import com.huatai.vba.common.utils.ListUtils;
import com.huatai.vba.dal.dao.VbaExtendsDAO;
import com.huatai.vba.service.VbaExtendsService;

@Service
public class VbaExtendsServiceImpl implements VbaExtendsService {

	protected static final Logger logger = LoggerFactory.getLogger(VbaExtendsService.class);

	@Autowired
	private VbaExtendsDAO vbaExtendsDAO;

	@Override
	public Integer executeSql(String sql) throws BusinessException {
		logger.info("executeSql:{}", sql);
		return vbaExtendsDAO.executeSql(sql);
	}

	@Override
	public List<Map<String, Object>> querySql(String sql) throws BusinessException {
		logger.info("querySql:{}", sql);
		return vbaExtendsDAO.querySql(sql);
	}

	@Override
	public boolean isTableExists(String tableName) throws BusinessException {
		String sql = "select 1 from user_objects where object_name='" + (tableName.toUpperCase())
				+ "' and object_type='Table'";
		return ListUtils.isNotEmpty(querySql(sql));
	}

	@Transactional
	public void cleanTable(String tableName) throws BusinessException {
		if (isTableExists(tableName)) {
			String sql = "delete from " + tableName;
			executeSql(sql);
		}
	}

}
